import { Router } from 'express';
import { ContentController } from '../../admin/controllers/contentController';
import { authenticate, authorize } from '../../middleware/auth';
import { ALL_PERMISSIONS } from '../../constants/permissions';
import { uploadBadgeIcon } from '../../middleware/upload';

const router = Router();
const contentController = new ContentController();

/**
 * @swagger
 * components:
 *   schemas:
 *     FunFact:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           description: 冷知识ID
 *         title:
 *           type: string
 *           description: 标题
 *         content:
 *           type: string
 *           description: 内容
 *         category:
 *           type: string
 *           description: 分类
 *         artistId:
 *           type: integer
 *           nullable: true
 *           description: 关联艺人ID
 *         isActive:
 *           type: boolean
 *           description: 是否激活
 *         createdAt:
 *           type: string
 *           format: date-time
 *         updatedAt:
 *           type: string
 *           format: date-time
 *         artist:
 *           $ref: '#/components/schemas/Artist'
 *     Badge:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           description: 徽章ID
 *         name:
 *           type: string
 *           description: 徽章名称
 *         description:
 *           type: string
 *           description: 徽章描述
 *         icon:
 *           type: string
 *           description: 徽章图标
 *         color:
 *           type: string
 *           description: 徽章颜色
 *         isActive:
 *           type: boolean
 *           description: 是否激活
 *         createdAt:
 *           type: string
 *           format: date-time
 *         updatedAt:
 *           type: string
 *           format: date-time
 */

/**
 * @swagger
 * /api/admin/content/trivia:
 *   get:
 *     summary: 获取冷知识列表
 *     tags: [Content Management]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 20
 *         description: 每页数量
 *       - in: query
 *         name: category
 *         schema:
 *           type: string
 *         description: 分类筛选
 *       - in: query
 *         name: artistId
 *         schema:
 *           type: integer
 *         description: 艺人ID筛选
 *       - in: query
 *         name: isActive
 *         schema:
 *           type: boolean
 *         description: 激活状态筛选
 *     responses:
 *       200:
 *         description: 成功获取冷知识列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 获取冷知识列表成功
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/FunFact'
 *                 pagination:
 *                   $ref: '#/components/schemas/Pagination'
 */
router.get('/trivia', authenticate, authorize([ALL_PERMISSIONS.CONTENT_READ]), contentController.getTrivia);

/**
 * @swagger
 * /api/admin/content/trivia/{id}:
 *   get:
 *     summary: 获取冷知识详情
 *     tags: [Content Management]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 冷知识ID
 *     responses:
 *       200:
 *         description: 成功获取冷知识详情
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 获取冷知识详情成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     funFact:
 *                       $ref: '#/components/schemas/FunFact'
 */
router.get('/trivia/:id', authenticate, authorize([ALL_PERMISSIONS.CONTENT_READ]), contentController.getTriviaById);

/**
 * @swagger
 * /api/admin/content/trivia:
 *   post:
 *     summary: 创建冷知识
 *     tags: [Content Management]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - title
 *               - content
 *             properties:
 *               title:
 *                 type: string
 *                 description: 标题
 *               content:
 *                 type: string
 *                 description: 内容
 *               category:
 *                 type: string
 *                 description: 分类
 *               artistId:
 *                 type: integer
 *                 nullable: true
 *                 description: 关联艺人ID
 *     responses:
 *       201:
 *         description: 成功创建冷知识
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 201
 *                 message:
 *                   type: string
 *                   example: 创建冷知识成功
 *                 data:
 *                   $ref: '#/components/schemas/FunFact'
 */
router.post('/trivia', authenticate, authorize([ALL_PERMISSIONS.CONTENT_CREATE]), contentController.createTrivia);

/**
 * @swagger
 * /api/admin/content/trivia/{id}:
 *   put:
 *     summary: 更新冷知识
 *     tags: [Content Management]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 冷知识ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               title:
 *                 type: string
 *                 description: 标题
 *               content:
 *                 type: string
 *                 description: 内容
 *               category:
 *                 type: string
 *                 description: 分类
 *               artistId:
 *                 type: integer
 *                 nullable: true
 *                 description: 关联艺人ID
 *               isActive:
 *                 type: boolean
 *                 description: 是否激活
 *     responses:
 *       200:
 *         description: 成功更新冷知识
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 更新冷知识成功
 *                 data:
 *                   $ref: '#/components/schemas/FunFact'
 */
router.put('/trivia/:id', authenticate, authorize([ALL_PERMISSIONS.CONTENT_UPDATE]), contentController.updateTrivia);

/**
 * @swagger
 * /api/admin/content/trivia/{id}:
 *   delete:
 *     summary: 删除冷知识
 *     tags: [Content Management]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 冷知识ID
 *     responses:
 *       200:
 *         description: 成功删除冷知识
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 删除冷知识成功
 */
router.delete('/trivia/:id', authenticate, authorize([ALL_PERMISSIONS.CONTENT_DELETE]), contentController.deleteTrivia);

/**
 * @swagger
 * /api/admin/content/badges:
 *   get:
 *     summary: 获取徽章列表
 *     tags: [Content Management]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 20
 *         description: 每页数量
 *       - in: query
 *         name: isActive
 *         schema:
 *           type: boolean
 *         description: 激活状态筛选
 *     responses:
 *       200:
 *         description: 成功获取徽章列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 获取徽章列表成功
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/Badge'
 *                 pagination:
 *                   $ref: '#/components/schemas/Pagination'
 */
router.get('/badges', authenticate, authorize([ALL_PERMISSIONS.BADGE_READ]), contentController.getBadges);

/**
 * @swagger
 * /api/admin/content/badges/{id}:
 *   get:
 *     summary: 获取徽章详情
 *     tags: [Content Management]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 徽章ID
 *     responses:
 *       200:
 *         description: 成功获取徽章详情
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 获取徽章详情成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     badge:
 *                       $ref: '#/components/schemas/Badge'
 */
router.get('/badges/:id', authenticate, authorize([ALL_PERMISSIONS.BADGE_READ]), contentController.getBadgeById);

/**
 * /api/admin/content/badges:
 *   post:
 *     summary: 创建徽章
 *     tags: [Content Management]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - description
 *             properties:
 *               name:
 *                 type: string
 *                 description: 徽章名称
 *               description:
 *                 type: string
 *                 description: 徽章描述
 *               icon:
 *                 type: string
 *                 description: 徽章图标
 *               color:
 *                 type: string
 *                 description: 徽章颜色
 *     responses:
 *       201:
 *         description: 成功创建徽章
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 201
 *                 message:
 *                   type: string
 *                   example: 创建徽章成功
 *                 data:
 *                   $ref: '#/components/schemas/Badge'
 */
router.post('/badges', authenticate, authorize([ALL_PERMISSIONS.BADGE_CREATE]), contentController.createBadge);

/**
 * @swagger
 * /api/admin/content/badges/{id}:
 *   put:
 *     summary: 更新徽章
 *     tags: [Content Management]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 徽章ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 徽章名称
 *               description:
 *                 type: string
 *                 description: 徽章描述
 *               icon:
 *                 type: string
 *                 description: 徽章图标
 *               color:
 *                 type: string
 *                 description: 徽章颜色
 *               isActive:
 *                 type: boolean
 *                 description: 是否激活
 *     responses:
 *       200:
 *         description: 成功更新徽章
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 更新徽章成功
 *                 data:
 *                   $ref: '#/components/schemas/Badge'
 */
router.put('/badges/:id', authenticate, authorize([ALL_PERMISSIONS.BADGE_UPDATE]), contentController.updateBadge);

/**
 * @swagger
 * /api/admin/content/badges/{id}:
 *   delete:
 *     summary: 删除徽章
 *     tags: [Content Management]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 徽章ID
 *     responses:
 *       200:
 *         description: 成功删除徽章
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 删除徽章成功
 */
router.delete('/badges/:id', authenticate, authorize([ALL_PERMISSIONS.BADGE_DELETE]), contentController.deleteBadge);

/**
 * @swagger
 * /api/admin/content/badges/{id}/icon:
 *   post:
 *     summary: 上传徽章图标
 *     tags: [Content Management]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 徽章ID
 *     requestBody:
 *       required: true
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             required:
 *               - badgeIcon
 *             properties:
 *               badgeIcon:
 *                 type: string
 *                 format: binary
 *                 description: 徽章图标文件
 *     responses:
 *       200:
 *         description: 成功上传徽章图标
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 徽章图标上传成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     badge:
 *                       $ref: '#/components/schemas/Badge'
 *                     iconUrl:
 *                       type: string
 *                       description: 图标URL
 */
router.post('/badges/:id/icon', 
  authenticate, 
  authorize([ALL_PERMISSIONS.BADGE_UPDATE]), 
  uploadBadgeIcon, 
  contentController.uploadBadgeIcon
);

/**
 * @swagger
 * /api/admin/content/badges/{id}/icon:
 *   delete:
 *     summary: 删除徽章图标
 *     tags: [Content Management]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 徽章ID
 *     responses:
 *       200:
 *         description: 成功删除徽章图标
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 徽章图标删除成功
 *                 data:
 *                   $ref: '#/components/schemas/Badge'
 */
router.delete('/badges/:id/icon', 
  authenticate, 
  authorize([ALL_PERMISSIONS.BADGE_UPDATE]), 
  contentController.deleteBadgeIcon
);

export default router; 